package com.ilop.sthome.utils.greenDao.utils;

import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.Log;

import com.example.xmpic.ui.data.CameraBean;
import com.example.xmpic.ui.utils.CameraDaoUtil;
import com.ilop.sthome.common.udp.GatewayUdpUtil;
import com.ilop.sthome.data.enums.DevType;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBeanDao;
import com.ilop.sthome.data.greenDao.RoomBean;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.ConfigureUtil;
import com.ilop.sthome.utils.greenDao.manage.CommonDaoUtils;
import com.ilop.sthome.utils.greenDao.manage.DaoManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeviceDaoUtil {

    private static final String TAG = DeviceDaoUtil.class.getSimpleName();

    private volatile static DeviceDaoUtil instance = null;
    private CommonDaoUtils<DeviceInfoBean> mDeviceUtils;
    private DeviceInfoBeanDao mDeviceDao;

    public static DeviceDaoUtil getInstance(){
        if (instance == null){
            synchronized (DeviceDaoUtil.class){
                if (instance == null){
                    instance = new DeviceDaoUtil();
                }
            }
        }
        return instance;
    }

    public static void setInstance(){
        instance = null;
    }

    private DeviceDaoUtil() {
        DaoManager mManager = DaoManager.getInstance();
        mDeviceDao= mManager.getDaoSession().getDeviceInfoBeanDao();
        mDeviceUtils = new CommonDaoUtils(DeviceInfoBean.class, mDeviceDao);
    }

    public CommonDaoUtils<DeviceInfoBean> getDeviceDao(){
        return mDeviceUtils;
    }

    /**
     * 新增网关
     * @param gateway 网关信息
     * @param roomId 网关房间
     */
    public void insertGateway(DeviceInfoBean gateway, String roomId){
        gateway.setDeviceOnly(gateway.getDeviceName()+"-0");
        gateway.setRoom_id(roomId);
        gateway.setDevice_ID(0);
        gateway.setDevice_type("GATEWAY");
        mDeviceDao.insertOrReplace(gateway);
    }

    /**
     * 更新已存在的网关
     * @param gateway 网关
     */
    public void updateGateway(DeviceInfoBean gateway){
        DeviceInfoBean deviceInfo = findGatewayByDeviceName(gateway.getDeviceName());
        if (deviceInfo == null){
            gateway.setDeviceOnly(gateway.getDeviceName()+"-0");
            gateway.setDevice_ID(0);
            gateway.setDevice_type("GATEWAY");
            gateway.setRoom_id("00");//新增时候加入到默认房间
            mDeviceDao.insert(gateway);
        }else {
            deviceInfo.setStatus(gateway.getStatus());
            deviceInfo.setNickName(gateway.getNickName());
            mDeviceDao.update(deviceInfo);
        }
    }

    /**
     * 同步云端网关数据
     */
    public void synGatewayData(List<String> dbOpenidList){
        List<String> allOpenidList = findAllGatewayDeviceName();
        if (dbOpenidList != null && !dbOpenidList.isEmpty()) {
            Map<String, String> dataMap = new HashMap<>();
            for (String id : dbOpenidList) {
                dataMap.put(id, id);
            }

            for (String id : allOpenidList) {
                if (!dataMap.containsKey(id)) {
                    deleteAllDataWithGateway(id);
                }
            }
        }
    }

    /**
     * 同步云端摄像头数据
     */
    public void synCameraData(List<String> dbOpenidList){
        List<String> allOpenidList = findAllCameraId();
        if (dbOpenidList != null && !dbOpenidList.isEmpty()) {
            Map<String, String> dataMap = new HashMap<>();
            for (String id : dbOpenidList) {
                dataMap.put(id, id);
            }
            for (String id : allOpenidList) {
                if (!dataMap.containsKey(id)) {
                    deleteCameraById(id);
                }
            }
        }
    }

    /**
     * 插入子设备
     * @param subDevice 子设备
     */
    public void insertSubDevice(DeviceInfoBean subDevice){
        mDeviceDao.insertOrReplace(subDevice);
    }

    /**
     * 插入摄像头
     * @param userId 唯一ID
     * @param value map
     */
    public void insertCamera(Context mContext,String userId, Map<String, String> value){
        DeviceInfoBean camera = new DeviceInfoBean();
        camera.setDeviceOnly(userId);
        camera.setDeviceName(value.get("name"));
        camera.setSubdeviceName(value.get("displayName"));
        String roomId  = value.get("birthday");
        if (roomId == null){
            roomId = "00";
            RoomDaoUtil.getInstance().deleteRoom(userId);
        }
        camera.setRoom_id(roomId);
        camera.setDevice_type("IPC");
        camera.setDevice_ID(-1);
        mDeviceDao.insertOrReplace(camera);
        insertCamera2(mContext, camera);
    }

    private void insertCamera2(Context mContext, DeviceInfoBean device){
        CameraBean cameraBean = new CameraBean();
        cameraBean.setUserId(device.getDeviceOnly());
        cameraBean.setDeviceId(device.getDeviceName());
        cameraBean.setDeviceName(device.getSubdeviceName());
        cameraBean.setRoomId(device.getRoom_id());
        if (device.getRoom_id() != null){
            RoomBean room = RoomDaoUtil.getInstance().getRoomByRoomId(device.getRoom_id());
            if (room!=null) {
                String roomName = room.getNickname()!=null?room.getNickname(): ConfigureUtil.roomName(mContext, room.getRid());
                cameraBean.setRoomName(roomName);
            }
        }
        CameraDaoUtil.getInstance().insertCamera(cameraBean);
    }

    /**
     * 保存或更新同步的子设备
     * @param deviceName 网关
     * @param deviceId 设备ID
     * @param deviceType 设备类型
     * @param status data_str2
     */
    public boolean updateSubDevice(String deviceName, int deviceId, String deviceType, String status, String roomId){
        DeviceInfoBean gateway = findGatewayByDeviceName(deviceName);
        if (gateway != null && deviceId > 0) {
            String deviceOnly = deviceName + "-" + deviceId;
            DeviceInfoBean subDevice = findByDeviceId(deviceName, deviceId);
            if (subDevice == null){
                DeviceInfoBean device = new DeviceInfoBean();
                device.setDeviceOnly(deviceOnly);
                device.setDevice_ID(deviceId);
                device.setDevice_type(deviceType);
                device.setDevice_status(status);
                device.setDeviceName(deviceName);
                device.setRoom_id(roomId);
                mDeviceDao.insert(device);
                return true;
            }else {
                subDevice.setDevice_type(deviceType);
                subDevice.setDevice_status(status);
                subDevice.setRoom_id(roomId);
                mDeviceDao.update(subDevice);
                return false;
            }
        }
        return false;
    }

    /**
     * 更新子设备名称
     * @param subDeviceName 昵称
     */
    public void updateSubDeviceName(String deviceName, String subDeviceName){
        String lastName = CoderUtils.getStringFromAscii(subDeviceName.substring(4));
        Log.i(TAG, "updateSubDeviceName: " + lastName);
        if (!TextUtils.isEmpty(lastName)){
            int deviceId = Integer.parseInt(subDeviceName.substring(0, 4), 16);
            DeviceInfoBean deviceInfoBean = findByDeviceId(deviceName, deviceId);
            if (deviceInfoBean != null){
                deviceInfoBean.setSubdeviceName(lastName);
                mDeviceUtils.update(deviceInfoBean);
            }
        }
    }

    /**
     * 更新网关下所有设备为离线状态
     * @param deviceName 网关
     */
    public void updateDeviceOffLine(String deviceName){
        List<DeviceInfoBean> mList = mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.DeviceName.eq(deviceName),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .orderAsc(DeviceInfoBeanDao.Properties.Device_ID)
                .build()
                .list();
        for (DeviceInfoBean device:mList) {
            device.setDevice_status("FFFFFFFF");
            mDeviceUtils.update(device);
        }
    }

    /**
     * 查询是否有网关或摄像头
     * @return 设备列表
     */
    public List<DeviceInfoBean> findGatewayAndCamera(){
        String productKey = DevType.EE_GATEWAY.getProductkey();
        return mDeviceDao.queryBuilder()
                .whereOr(DeviceInfoBeanDao.Properties.ProductKey.eq(productKey),
                        DeviceInfoBeanDao.Properties.Device_type.eq("IPC"))
                .build()
                .list();
    }

    /**
     * 查找所有设备
     */
    public List<DeviceInfoBean> findAllDevice(){
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .orderAsc(DeviceInfoBeanDao.Properties.Device_ID)
                .build()
                .list();
    }

    /**
     * 查找网关和网关下的子设备
     */
    public List<DeviceInfoBean> findAllDeviceOrderByGateway(){
        List<DeviceInfoBean> cameraList= findAllCamera();
        List<DeviceInfoBean> gatewayList = findAllGateway();
        List<DeviceInfoBean> deviceList = new ArrayList<>(cameraList);
        if (gatewayList.size()>0){
            for (DeviceInfoBean gateway:gatewayList) {
                deviceList.add(gateway);
                List<DeviceInfoBean> subDeviceList = findAllSubDevice(gateway.getDeviceName());
                deviceList.addAll(subDeviceList);
            }
        }
        return deviceList;
    }

    /**
     * 查找网关设备
     */
    public List<DeviceInfoBean> findAllGateway(){
        String productKey = DevType.EE_GATEWAY.getProductkey();
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.Device_ID.eq(0),
                        DeviceInfoBeanDao.Properties.ProductKey.eq(productKey),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .orderAsc(DeviceInfoBeanDao.Properties.GmtModified)
                .build()
                .list();
    }

    /**
     * 查找所有摄像头设备
     * @return DeviceInfoBean
     */
    public List<DeviceInfoBean> findAllCamera(){
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.Device_type.eq("IPC"),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .build()
                .list();
    }

    /**
     * 查找网关设备
     * @return DeviceInfoBean
     */
    public DeviceInfoBean findGatewayByDeviceName(String deviceName){
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.DeviceName.eq(deviceName),
                        DeviceInfoBeanDao.Properties.Device_ID.eq(0),
                        DeviceInfoBeanDao.Properties.ProductKey.isNotNull(),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .build()
                .unique();

    }

    /**
     * 查找设备
     * @return DeviceInfoBean
     */
    public DeviceInfoBean findByDeviceId(String deviceName, int deviceId){
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.DeviceName.eq(deviceName),
                        DeviceInfoBeanDao.Properties.Device_ID.eq(deviceId),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .orderAsc(DeviceInfoBeanDao.Properties.Device_ID)
                .build()
                .unique();
    }

    /**
     * 查找所有子设备
     * @return list
     */
    public List<DeviceInfoBean> findAllSubDevice(){
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.Device_ID.gt(0),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .orderAsc(DeviceInfoBeanDao.Properties.Device_ID)
                .build()
                .list();
    }

    /**
     * 查找网关下所有子设备
     * @return list
     */
    public List<DeviceInfoBean> findAllSubDevice(String deviceName){
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.Device_ID.gt(0),
                        DeviceInfoBeanDao.Properties.DeviceName.eq(deviceName),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .orderAsc(DeviceInfoBeanDao.Properties.Device_ID)
                .build()
                .list();
    }

    /**
     * 通过UserId查找摄像头设备
     * @return DeviceInfoBean
     */
    public DeviceInfoBean findCameraByCameraId(String cameraId){
        List<DeviceInfoBean> mList =  mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.Device_type.eq("IPC"),
                        DeviceInfoBeanDao.Properties.DeviceName.eq(cameraId),
                        DeviceInfoBeanDao.Properties.Device_ID.eq(-1),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .build()
                .list();
        if (mList.size()>0){
            return mList.get(0);
        }else {
            return null;
        }
    }

    /**
     * 查找网关除去分享
     * @return list
     */
    public List<DeviceInfoBean> findAllGatewayNotShare(){
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.Owned.eq(1),
                        DeviceInfoBeanDao.Properties.Device_ID.eq(0),
                        DeviceInfoBeanDao.Properties.ProductKey.isNotNull(),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .orderAsc(DeviceInfoBeanDao.Properties.GmtModified)
                .build()
                .list();
    }

    /**
     * 查找所有网关设备的deviceName
     * @return deviceName
     */
    public List<String> findAllGatewayDeviceName(){
        List<DeviceInfoBean> mList = mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.ProductKey.isNotNull(),
                        DeviceInfoBeanDao.Properties.Device_ID.eq(0),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .build()
                .list();
        List<String> deviceName = new ArrayList<>();
        for (DeviceInfoBean device:mList) {
            deviceName.add(device.getDeviceName());
        }
        return deviceName;
    }

    /**
     * 查找所有网关设备的deviceName
     * @return deviceName
     */
    public List<String> findAllCameraId(){
        List<DeviceInfoBean> mList = findAllCamera();
        List<String> cameraId = new ArrayList<>();
        for (DeviceInfoBean device:mList) {
            cameraId.add(device.getDeviceName());
        }
        return cameraId;
    }

    /**
     * 查找输入设备
     * @param deviceName 网关
     * @return
     */
    public List<DeviceInfoBean> findInputDevice(String deviceName){
        List<DeviceInfoBean> mDeviceInfo = new ArrayList<>();
        try {
            String sql = "select * from DEVICE_INFO_BEAN where DEVICE_NAME = '" + deviceName + "' and (DEVICE_TYPE GLOB '*0??' or DEVICE_TYPE GLOB '*1??' or DEVICE_TYPE GLOB '*3??' or DEVICE_TYPE GLOB '*6??' or DEVICE_TYPE = '1213') ORDER BY DEVICE__ID";
            Cursor cursor = DaoManager.getInstance().getDaoSession().getDatabase().rawQuery(sql, null);
            while (cursor.moveToNext()){
                DeviceInfoBean device = new DeviceInfoBean();
                device.setDeviceName(cursor.getString(cursor.getColumnIndex("DEVICE_NAME")));
                device.setSubdeviceName(cursor.getString(cursor.getColumnIndex("SUBDEVICE_NAME")));
                device.setDevice_status(cursor.getString(cursor.getColumnIndex("DEVICE_STATUS")));
                device.setDevice_type(cursor.getString(cursor.getColumnIndex("DEVICE_TYPE")));
                device.setDevice_ID(cursor.getInt(cursor.getColumnIndex("DEVICE__ID")));
                mDeviceInfo.add(device);
            }
            cursor.close();
        }catch (NullPointerException e){
            Log.e(TAG, "findInputDevice: "+ e.getMessage());
        }
        return mDeviceInfo;
    }

    /**
     * 查找输出设备
     * 除去361部分
     * @param deviceName 网关
     * @return
     */
    public List<DeviceInfoBean> findOutputDevice(String deviceName){
        List<DeviceInfoBean> mDeviceInfo = new ArrayList<>();
        try {
            String sql = "select * from DEVICE_INFO_BEAN where DEVICE_NAME = '" + deviceName + "' and DEVICE_TYPE GLOB '*2??' and DEVICE_TYPE !='1213' ORDER BY DEVICE__ID";
            Cursor cursor = DaoManager.getInstance().getDaoSession().getDatabase().rawQuery(sql, null);
            while (cursor.moveToNext()){
                DeviceInfoBean device = new DeviceInfoBean();
                device.setDeviceName(cursor.getString(cursor.getColumnIndex("DEVICE_NAME")));
                device.setSubdeviceName(cursor.getString(cursor.getColumnIndex("SUBDEVICE_NAME")));
                device.setDevice_status(cursor.getString(cursor.getColumnIndex("DEVICE_STATUS")));
                device.setDevice_type(cursor.getString(cursor.getColumnIndex("DEVICE_TYPE")));
                device.setDevice_ID(cursor.getInt(cursor.getColumnIndex("DEVICE__ID")));
                mDeviceInfo.add(device);
            }
            cursor.close();
        }catch (NullPointerException e){
            Log.e(TAG, "findOutputDevice: "+ e.getMessage());
        }
        return mDeviceInfo;
    }

    /**
     * 根据设备类型查询
     * @param deviceName 网关
     * @param deviceType 子设备类型
     * @return 设备
     */
    public List<DeviceInfoBean> findDeviceByType(String deviceName, String deviceType){
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.DeviceName.eq(deviceName),
                        DeviceInfoBeanDao.Properties.Device_type.eq(deviceType),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .orderAsc(DeviceInfoBeanDao.Properties.Device_ID)
                .build()
                .list();
    }

    /**
     * 根据设备类型查询
     * @param deviceType 子设备类型
     * @return 设备
     */
    public List<DeviceInfoBean> findDeviceByType(String deviceType){
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.Device_type.eq(deviceType),
                        DeviceInfoBeanDao.Properties.Device_ID.gt(0),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .orderAsc(DeviceInfoBeanDao.Properties.Device_ID)
                .build()
                .list();
    }

     /**
     * 根据条件删除
     */
    public void deleteAllGateway(){
        mDeviceUtils.deleteByQuery(DeviceInfoBeanDao.Properties.ProductKey.isNotNull(),
                DeviceInfoBeanDao.Properties.Device_ID.eq(0));
    }

    /**
     * 根据条件删除网关下所有子设备
     */
    public void deleteAllDeviceByGateway(String deviceName){
        mDeviceUtils.deleteByQuery(DeviceInfoBeanDao.Properties.DeviceName.eq(deviceName));
    }

    /**
     * 删除所有摄像头
     */
    public void deleteAllCamera(){
        mDeviceUtils.deleteByQuery(DeviceInfoBeanDao.Properties.Device_type.eq("IPC"),
                DeviceInfoBeanDao.Properties.Device_ID.eq(-1));
        CameraDaoUtil.getInstance().deleteAllCamera();
    }

    /**
     * 根据条件删除摄像头
     * @param cameraId 序列号
     */
    public void deleteCameraById(String cameraId){
        mDeviceUtils.deleteByQuery(DeviceInfoBeanDao.Properties.Device_type.eq("IPC"),
                DeviceInfoBeanDao.Properties.DeviceName.eq(cameraId),
                DeviceInfoBeanDao.Properties.Device_ID.eq(-1));
        CameraDaoUtil.getInstance().deleteCameraById(cameraId);
    }

    /**
     * 根据条件删除
     * @param deviceName 网关
     * @param deviceId 子设备ID
     */
    public void deleteByDeviceName(String deviceName, int deviceId){
        mDeviceUtils.deleteByQuery(DeviceInfoBeanDao.Properties.DeviceName.eq(deviceName),
                DeviceInfoBeanDao.Properties.Device_ID.eq(deviceId));
    }

    /**
     * 删除网关下所有数据
     * @param deviceName 网关
     */
    public void deleteAllDataWithGateway(String deviceName){
        deleteAllDeviceByGateway(deviceName);
        SceneDaoUtil.getInstance().deleteByDeviceName(deviceName);
        AutomationDaoUtil.getInstance().deleteByDeviceName(deviceName);
        DefaultDaoUtil.getInstance().deleteByDeviceName(deviceName);
        GatewayUdpUtil.getInstance().setAllGatewayOffLine();
    }

    /**
     * 删除除摄像头外的所有设备
     */
    public void deleteAllDataWithoutCamera(){
        mDeviceUtils.deleteByQuery(DeviceInfoBeanDao.Properties.Device_ID.gt(-1),
                DeviceInfoBeanDao.Properties.Device_type.notEq("IPC"));
    }

    /**
     * 清空设备相关所有数据（除了摄像头）
     */
    public void deleteAllDataExceptCamera(){
        deleteAllDataWithoutCamera();
        SceneDaoUtil.getInstance().getSceneDao().deleteAll();
        AutomationDaoUtil.getInstance().getAutomationDao().deleteAll();
        DefaultDaoUtil.getInstance().getDefaultDao().deleteAll();
        GatewayUdpUtil.getInstance().setAllGatewayOffLine();
    }

    /**
     * 查询房间内的所有设备
     * @param roomId 房间ID
     * @return list
     */
    public List<DeviceInfoBean> findDeviceIntoRoom(String roomId){
        List<DeviceInfoBean> mDevice =  mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.Room_id.eq(roomId),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull())
                .orderAsc(DeviceInfoBeanDao.Properties.Device_ID)
                .build()
                .list();
        int shareSize = findShareGatewayNumber();
        if (shareSize>0){
            List<DeviceInfoBean> mShare = findShareDeviceInRoom(roomId);
            if (mShare.size()>0){
                mDevice.removeAll(mShare);
            }
        }
        return mDevice;
    }

    /**
     * 查询所有在此房间中的分享设备
     * @param roomId 房间ID
     * @return 分享设备
     */
    private List<DeviceInfoBean> findShareDeviceInRoom(String roomId){
        List<DeviceInfoBean> newList = new ArrayList<>();
        List<DeviceInfoBean> allShareList = findAllShareDevice();
        for (DeviceInfoBean device:allShareList) {
            if (roomId.equals(device.getRoom_id())){
                newList.add(device);
            }
        }
        return newList;
    }

    /**
     * 查询所有分享来的设备
     * @return shareList
     */
    public List<DeviceInfoBean> findAllShareDevice(){
        List<DeviceInfoBean> mGatewayList = mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.ProductKey.isNotNull(),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull(),
                        DeviceInfoBeanDao.Properties.Device_ID.eq(0),
                        DeviceInfoBeanDao.Properties.Owned.notEq(1))
                .build().list();
        List<DeviceInfoBean> mDevice = new ArrayList<>();
        for (DeviceInfoBean gateway:mGatewayList) {
            mDevice.add(gateway);
            List<DeviceInfoBean> mSubDevice = findAllSubDevice(gateway.getDeviceName());
            if (mSubDevice.size()>0) {
                mDevice.addAll(mSubDevice);
            }
        }
        return mDevice;
    }

    /**
     * 查询所有分享网关数量
     * @return number
     */
    int findShareGatewayNumber(){
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.ProductKey.isNotNull(),
                        DeviceInfoBeanDao.Properties.DeviceOnly.isNotNull(),
                        DeviceInfoBeanDao.Properties.Device_ID.eq(0),
                        DeviceInfoBeanDao.Properties.Owned.notEq(1))
                .build().list().size();
    }

    /**
     * 判断此种类型的设备是否存在
     * @param deviceType 设备类型
     * @return int
     */
    public int isDevTypeExists(String deviceType){
        if(TextUtils.isEmpty(deviceType) || deviceType.length()!=4){
            return 0;
        }
        return mDeviceDao.queryBuilder()
                .where(DeviceInfoBeanDao.Properties.Device_type.eq(deviceType))
                .list()
                .size();
    }
}
